/* drivers/video/goldfishfb.c
**
** Copyright (C) 2007 Google, Inc.
**
** This software is licensed under the terms of the GNU General Public
** License version 2, as published by the Free Software Foundation, and
** may be copied, distributed, and modified under those terms.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
*/

#include <asm-arm/irq.h>
//#include <goldfish/bitops.h>	
#include <goldfish/dma.h>
#include <asm-arm/io.h>
#include <goldfish/head.h>	
#include <goldfish/goldfishfb.h>	
#include <asm-arm/errno-base.h>
#include <asm-arm/arch-goldfish/hardware.h>

#include <xen/sched.h>
#include <asm-arm/current.h>
#include <xen/event.h>
#include <public/goldfish_drivers_types.h>

enum {
	FB_GET_WIDTH        = 0x00,
	FB_GET_HEIGHT       = 0x04,
	FB_INT_STATUS       = 0x08,
	FB_INT_ENABLE       = 0x0c,
	FB_SET_BASE         = 0x10,
	FB_SET_ROTATION     = 0x14,
	FB_SET_BLANK        = 0x18,
	FB_GET_PHYS_WIDTH   = 0x1c,
	FB_GET_PHYS_HEIGHT  = 0x20,

	FB_INT_VSYNC             = 1U << 0,
	FB_INT_BASE_UPDATE_DONE  = 1U << 1
};


static int
goldfish_fb_interrupt(int irq, void *dev_id)
{
	unsigned long irq_flags;
	struct goldfish_fb	*fb = dev_id;
	uint32_t status;
	
	spin_lock_irqsave(&fb->lock, irq_flags);
	status = __raw_readl(fb->reg_base + FB_INT_STATUS);
	if(status & FB_INT_BASE_UPDATE_DONE) {
		fb->base_update_count++;//printk("fb->base_update_count=%d\n", fb->base_update_count);
	//modify wake_up(&fb->wait);
	}
	spin_unlock_irqrestore(&fb->lock, irq_flags);
	
	if( !is_idle_vcpu(current) && current->virq_to_evtchn[VIRQ_FB] != 0  )
	{
		printk("Xen goldfish_fb_interrupt,status=0x%x\n",status);
		send_guest_virq(current, VIRQ_FB);
		if(current->domain->status)
			*(current->domain->status)=status;
	//	status = IRQ_HANDLED;
	}
	
	return status ? IRQ_HANDLED : IRQ_NONE;
}

static inline u32 convert_bitfield(int val, struct fb_bitfield *bf)
{
	unsigned int mask = (1 << bf->length) - 1;

	return (val >> (16 - bf->length) & mask) << bf->offset;
}

static int
goldfish_fb_setcolreg(unsigned int regno, unsigned int red, unsigned int green,
		 unsigned int blue, unsigned int transp, struct fb_info *info)
{
	struct goldfish_fb *fb = container_of(info, struct goldfish_fb, fb);

	if (regno < 16) {
		fb->cmap[regno] = convert_bitfield(transp, &fb->fb.var.transp) |
				  convert_bitfield(blue, &fb->fb.var.blue) |
				  convert_bitfield(green, &fb->fb.var.green) |
				  convert_bitfield(red, &fb->fb.var.red);
		return 0;
	}
	else {
		return 1;
	}
}

static int goldfish_fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
{
	if((var->rotate & 1) != (info->var.rotate & 1)) {
		if((var->xres != info->var.yres) ||
		   (var->yres != info->var.xres) ||
		   (var->xres_virtual != info->var.yres) ||
		   (var->yres_virtual > info->var.xres * 2) ||
		   (var->yres_virtual < info->var.xres )) {
			return -EINVAL;
		}
	}
	else {
		if((var->xres != info->var.xres) ||
		   (var->yres != info->var.yres) ||
		   (var->xres_virtual != info->var.xres) ||
		   (var->yres_virtual > info->var.yres * 2) ||
		   (var->yres_virtual < info->var.yres )) {
			return -EINVAL;
		}
	}
	if((var->xoffset != info->var.xoffset) ||
	   (var->bits_per_pixel != info->var.bits_per_pixel) ||
	   (var->grayscale != info->var.grayscale)) {
		return -EINVAL;
	}
	return 0;
}

static int goldfish_fb_set_par(struct fb_info *info)
{
	struct goldfish_fb *fb = container_of(info, struct goldfish_fb, fb);
	if(fb->rotation != fb->fb.var.rotate) {
		info->fix.line_length = info->var.xres * 2;
		fb->rotation = fb->fb.var.rotate;
		__raw_writel(fb->rotation, fb->reg_base + FB_SET_ROTATION);
	}
	return 0;
}


int goldfish_fb_pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
{
	unsigned long irq_flags, addr;
	int base_update_count;
	struct goldfish_fb *fb = container_of(info, struct goldfish_fb, fb);

	spin_lock_irqsave(&fb->lock, irq_flags);
	base_update_count = fb->base_update_count;//printk("base_update_count=%d irq_flags=%x\n", base_update_count, irq_flags);
	__raw_writel(fb->fb.fix.smem_start + fb->fb.var.xres * 2 * var->yoffset, fb->reg_base + FB_SET_BASE);
	spin_unlock_irqrestore(&fb->lock, irq_flags);
	//modify wait_event_timeout(fb->wait, fb->base_update_count != base_update_count, HZ / 15);
	
	if(fb->base_update_count == base_update_count)
		printk("goldfish_fb_pan_display: timeout wating for base update\n");
	return 0;
}

#ifdef CONFIG_ANDROID_POWER
static void goldfish_fb_early_suspend(android_early_suspend_t *h)
{
	struct goldfish_fb *fb = container_of(h, struct goldfish_fb, early_suspend);
	__raw_writel(1, fb->reg_base + FB_SET_BLANK);
}

static void goldfish_fb_late_resume(android_early_suspend_t *h)
{
	struct goldfish_fb *fb = container_of(h, struct goldfish_fb, early_suspend);
        __raw_writel(0, fb->reg_base + FB_SET_BLANK);
}
#endif

struct fb_ops goldfish_fb_ops = {
	//modify .owner          = THIS_MODULE,
	.fb_check_var   = goldfish_fb_check_var,
	.fb_set_par     = goldfish_fb_set_par,
	.fb_setcolreg   = goldfish_fb_setcolreg,
	.fb_pan_display = goldfish_fb_pan_display,
	.fb_fillrect    = cfb_fillrect,
	.fb_copyarea    = cfb_copyarea,
	.fb_imageblit   = cfb_imageblit,
};

struct goldfish_fb *goldfishfb = NULL;
unsigned int fbdom_dmabase, fbdom_paddr;

static int goldfish_fb_probe(struct platform_device *pdev)
{
	int ret;
	struct resource *r;
	struct goldfish_fb *fb;
	size_t framesize;
	uint32_t width, height;
	dma_addr_t fbpaddr;

	fb = xmalloc(struct goldfish_fb);
	if(fb == NULL) {
		ret = -ENOMEM;
		goto err_fb_alloc_failed;
	}
	spin_lock_init(&fb->lock);
	//modify init_waitqueue_head(&fb->wait);
	//modify platform_set_drvdata(pdev, fb);
	//printk("before get resource\n");
	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if(r == NULL) {
		ret = -ENODEV;
		goto err_no_io_base;
	}
	fb->reg_base = IO_ADDRESS(r->start - IO_START);

	fb->irq = platform_get_irq(pdev, 0);
	if(fb->irq < 0) {
		ret = -ENODEV;
		goto err_no_irq;
	}

	width = __raw_readl(fb->reg_base + FB_GET_WIDTH);
	height = __raw_readl(fb->reg_base + FB_GET_HEIGHT);
	printk("width:%u,height:%u\n",width,height);

	fb->fb.fbops		= &goldfish_fb_ops;
	fb->fb.flags		= FBINFO_FLAG_DEFAULT;
	fb->fb.pseudo_palette	= fb->cmap;
	//strncpy(fb->fb.fix.id, clcd_name, sizeof(fb->fb.fix.id));
	fb->fb.fix.type		= FB_TYPE_PACKED_PIXELS;
	fb->fb.fix.visual = FB_VISUAL_TRUECOLOR;
	fb->fb.fix.line_length = width * 2;
	//fb->fb.fix.line_length = width * 3;
	fb->fb.fix.accel	= FB_ACCEL_NONE;
	fb->fb.fix.ypanstep = 1;

	fb->fb.var.xres		= width;
	fb->fb.var.yres		= height;
	fb->fb.var.xres_virtual	= width;
	fb->fb.var.yres_virtual	= height * 2;
	//fb->fb.var.yres_virtual	= height * 3;
	fb->fb.var.bits_per_pixel = 16;
	fb->fb.var.activate	= FB_ACTIVATE_NOW;
	fb->fb.var.height	= __raw_readl(fb->reg_base + FB_GET_PHYS_HEIGHT);
	fb->fb.var.width	= __raw_readl(fb->reg_base + FB_GET_PHYS_WIDTH);
	printk("phys_width:%u,phys_height:%u\n",fb->fb.var.width,fb->fb.var.height);

	fb->fb.var.red.offset = 11;
	fb->fb.var.red.length = 5;
	fb->fb.var.green.offset = 5;
	fb->fb.var.green.length = 6;
	fb->fb.var.blue.offset = 0;
	fb->fb.var.blue.length = 5;

	/*fb->fb.var.red.offset = 16;
	fb->fb.var.red.length = 8;
	fb->fb.var.green.offset = 8;
	fb->fb.var.green.length = 8;
	fb->fb.var.blue.offset = 0;
	fb->fb.var.blue.length = 8;*/

	framesize = width * height * 2 ;
	fb->fb.screen_base = dma_alloc_writecombine(/*&pdev->dev, */framesize,
	                                            &fbpaddr/*, GFP_KERNEL*/);
	fbdom_dmabase = (u32)dma_alloc_writecombine(framesize, &fbdom_paddr);
	
	printk("allocating frame buffer %d * %d, got %p\n", width, height, fb->fb.screen_base);
	if(fb->fb.screen_base == 0) {
		ret = -ENOMEM;
		goto err_alloc_screen_base_failed;
	}
	//fbpaddr is a physical address related to frame buffer in memory address space
	fb->fb.fix.smem_start = fbpaddr;
	fb->fb.fix.smem_len = framesize;
	printk("fb->fb.screen_base:%x,fbpaddr:%x\n",fb->fb.screen_base,fbpaddr);

	ret = fb_set_var(&fb->fb, &fb->fb.var);
	printk("bits per pixel : %u\n",fb->fb.var.bits_per_pixel);
	if(ret)
		goto err_fb_set_var_failed;

	ret = request_irq(fb->irq, goldfish_fb_interrupt, IRQF_SHARED, pdev->name, fb);
	if(ret)
		goto err_request_irq_failed;

	__raw_writel(FB_INT_BASE_UPDATE_DONE, fb->reg_base + FB_INT_ENABLE);
	goldfish_fb_pan_display(&fb->fb.var, &fb->fb); // updates base of frame buffer
	printk("after display bits per pixel : %u\n",fb->fb.var.bits_per_pixel);
	/*ret = register_framebuffer(&fb->fb);
	if(ret)
		goto err_register_framebuffer_failed;*/

#ifdef CONFIG_ANDROID_POWER
	fb->early_suspend.suspend = goldfish_fb_early_suspend;
	fb->early_suspend.resume = goldfish_fb_late_resume;
	android_register_early_suspend(&fb->early_suspend);
#endif

	goldfishfb = fb;
	return 0;


err_register_framebuffer_failed:
	//free_irq(fb->irq, fb);
err_request_irq_failed:
err_fb_set_var_failed:
	printk("[ERROR] set var failed\n");
	//dma_free_writecombine(&pdev->dev, framesize, fb->fb.screen_base, fb->fb.fix.smem_start);
err_alloc_screen_base_failed:
err_no_irq:
err_no_io_base:
	xfree(fb);
err_fb_alloc_failed:
	return ret;
}

/*static int goldfish_fb_remove(struct platform_device *pdev)
{
	size_t framesize;
	struct goldfish_fb *fb = platform_get_drvdata(pdev);
	
	framesize = fb->fb.var.xres_virtual * fb->fb.var.yres_virtual * 2;

#ifdef CONFIG_ANDROID_POWER
        android_unregister_early_suspend(&fb->early_suspend);
#endif
	unregister_framebuffer(&fb->fb);
	free_irq(fb->irq, fb);
	dma_free_writecombine(&pdev->dev, framesize, fb->fb.screen_base, fb->fb.fix.smem_start);
	kfree(fb);
	return 0;
}*/


struct platform_driver goldfish_fb_driver = {
	.probe		= goldfish_fb_probe,
	//.remove		= goldfish_fb_remove,
	/*.driver = {
		.name = "goldfish_fb"
	}*/
};

/*static int __init goldfish_fb_init(void)
{
	return platform_driver_register(&goldfish_fb_driver);
}

static void __exit goldfish_fb_exit(void)
{
	platform_driver_unregister(&goldfish_fb_driver);
}

module_init(goldfish_fb_init);
module_exit(goldfish_fb_exit);

*/
